# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import gtk
import Plugin
import random
import time
import gobject

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    
    def __init__(self, controller, msn):
        '''Contructor'''
        Plugin.Plugin.__init__(self, controller, msn)
        
        self.description = _('Random personal message')
        self.authors = { 'Ikaroweb' : 'Ikaroweb@gmail.com'}
        self.website = 'http://www.p2pdoctor.it'
        self.displayName = _('RotatePm')
        self.name = 'Random Personal Message'
        self.controller = controller

	self.customConfigWidget = None
	self.configWindow = None
        self.config = controller.config
        self.config.readPluginConfig(self.name)

	self.config.getPluginValue( self.name, 'style', \
            'Personal Message' )
	

	

    def start(self):
        '''start the plugin'''
	#self.player.start()
        self.enabled = True
	self.message = 0
	i = random.randint(1, 4)
	
	if i == 1:
		msg = 'style_1'
	elif i == 2:
		msg = 'style_2'
	elif i == 3:
		msg = 'style_3'
	else:
		msg = 'style_4'

	self.msn.changePersonalMessage(self.config.getPluginValue(self.name, msg, ''))

	#self.RandomPM(0)
	self.interval = 5000
	self.message = gobject.timeout_add(self.interval, self.RandomPM, 0)

    def stop(self):    
        '''stop the plugin'''
	#self.config.setPluginValue(self.name,style,style)
        self.enabled = False
	gobject.source_remove(self.message)
        
    def check(self):
	
        return (True, 'Ok')

    def configure(self):
	style = self.config.getPluginValue(self.name, 'style_1', \
            'Personal Message')
	style_2 = self.config.getPluginValue(self.name, 'style_2', \
            'Personal Message')
	style_3 = self.config.getPluginValue(self.name, 'style_3', \
            'Personal Message')
	style_4 = self.config.getPluginValue(self.name, 'style_4', \
            'Personal Message')
	l = []
 	l.append(Plugin.Option('style', str, _('1 - Personal Message'), \
            _('Set Personal Message'), style))

	l.append(Plugin.Option('style_2', str, _('2 - Personal Message'), \
            _('Set Personal Message'), style_2))

	l.append(Plugin.Option('style_3', str, _('3 - Personal Message'), \
            _('Set Personal Message'), style_3))

	l.append(Plugin.Option('style_4', str, _('4 - Personal Message'), \
           _('Set Personal Message'), style_4))
	
	self.configWindow = Plugin.ConfigWindow( _( 'Config Plugin' ), l )
        #self.customConfigWidget = custom.widget
        response = self.configWindow.run()

	if response != None:
             
            if response.has_key( 'style' ):
                
                self.config.setPluginValue( self.name, 'style_1', \
                    response['style'].value )
		self.config.setPluginValue( self.name, 'style_2', \
                    response['style_2'].value )
		self.config.setPluginValue( self.name, 'style_3', \
                    response['style_3'].value )
		self.config.setPluginValue( self.name, 'style_4', \
                    response['style_4'].value )
		
            
        self.configWindow = None
        self.statusLabel = None

	return True

    def RandomPM(self , j=0):
	
	i = random.randint(1, 4)
	if i == 1:
		msg = 'style_1'
	elif i == 2:
		msg = 'style_2'
	elif i == 3:
		msg = 'style_3'
	else:
		msg = 'style_4'
	self.msn.changePersonalMessage(self.config.getPluginValue(self.name, msg, ''))
	return True
	
